Imports System.ComponentModel

Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.Utils

Imports Phoenix.Events

Namespace Forms.Controls

    Public Class FieldEditBase
        Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub

#End Region

#Region " Field Editor Behavior "
        Public Overridable ReadOnly Property FieldView() As DevExpress.XtraEditors.PopupContainerEdit
            Get
                Throw New InvalidOperationException
            End Get
        End Property


        Public Overridable ReadOnly Property GridView() As DevExpress.XtraGrid.Views.Grid.GridView
            Get
                Throw New InvalidOperationException
            End Get
        End Property

        Public Overridable ReadOnly Property PrimaryColumn() As DevExpress.XtraGrid.Columns.GridColumn
            Get
                Throw New InvalidOperationException
            End Get
        End Property

        Public Overridable ReadOnly Property Icon() As DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox
            Get
                Return Nothing
            End Get
        End Property

        Public Overridable ReadOnly Property IconColumn() As DevExpress.XtraGrid.Columns.GridColumn
            Get
                Return Nothing
            End Get
        End Property

        Public Overridable ReadOnly Property PopupWindow() As DevExpress.XtraEditors.PopupContainerControl
            Get
                Return Nothing
            End Get
        End Property

        Protected Overridable Sub ConfigureControl()
            'FieldView properties
            FieldView.Properties.CloseOnLostFocus = True
            FieldView.Properties.CloseOnOuterMouseClick = False
            FieldView.Properties.CloseUpKey = New KeyShortcut(Keys.Enter)

            'GridView properties
            GridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus
            GridView.GroupFormat = "{1}"
            GridView.OptionsView.ShowFilterPanel = False
        End Sub

        Public Property EditValue() As Object
            Get
                Return FieldView.EditValue
            End Get
            Set(ByVal Value As Object)
                FieldView.EditValue = Value
            End Set
        End Property


        Public Event EditValueChanged(ByVal Sender As Object, ByVal e As EventArgs)
        Protected Overridable Sub OnEditValueChanged(ByVal e As EventArgs)
            RaiseEvent EditValueChanged(Me, e)
        End Sub

#End Region

#Region " Event Handlers & Overrides "
        Private Sub Form_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            If DesignMode Then Return

            ConfigureControl()

            PopupWindow.Width = FieldView.Width

            AddHandler GridView.FocusedRowChanged, AddressOf GridView_FocusedRowChanged
            AddHandler GridView.DoubleClick, AddressOf GridView_DoubleClick
            AddHandler GridView.GotFocus, AddressOf GridView_GotFocus
            AddHandler FieldView.KeyPress, AddressOf FieldView_KeyPress
            AddHandler FieldView.EditValueChanged, AddressOf FieldView_EditValueChanged
            AddHandler PhoenixApplication.AppNotification, AddressOf Application_Notification

            UpdateConfiguration()
        End Sub

        Private Sub Application_Notification(ByVal sender As Object, ByVal args As PhoenixEventArgs)
            Select Case args.EventCode
                Case PhoenixEventCode.ConfigurationChanged
                    UpdateConfiguration()
            End Select
        End Sub

        Public Overridable Sub UpdateConfiguration()
            If Not Icon Is Nothing Then
                Icon.SmallImages = AppSharedComponents.ListImages
                IconColumn.MinWidth = Icon.SmallImages.ImageSize.Width + AppIconColumnPadding
            End If
        End Sub

        Private Sub FieldView_EditValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            OnEditValueChanged(e)
        End Sub

        Private Sub FieldView_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            If FieldView.IsPopupOpen Then Return

            FieldView.ShowPopup()
            GridView.FocusedColumn = PrimaryColumn
            GridView.StartIncrementalSearch(e.KeyChar.ToString())
        End Sub


        Private Sub GridView_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs)
            If FieldView.IsPopupOpen Then
                FieldView.EditValue = GridView.GetDataRow(e.FocusedRowHandle)
            End If
        End Sub

        Private Sub GridView_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
            If FieldView.EditValue Is Nothing Then
                GridView.FocusedRowHandle = GridControl.InvalidRowHandle
            Else
                Dim val As DataRow = CType(FieldView.EditValue, DataRow)
                For i As Integer = 0 To GridView.DataRowCount
                    Dim r As DataRow = GridView.GetDataRow(i)
                    If Data.AreDataRowsSame(r, val) Then
                        GridView.FocusedRowHandle = i
                        Return
                    End If
                Next
            End If
        End Sub

        Private Sub GridView_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim p As Point = GridView.GridControl.PointToClient(Control.MousePosition)
            Dim hi As GridHitInfo = GridView.CalcHitInfo(p)
            Select Case hi.HitTest
                Case GridHitTest.Row
                Case GridHitTest.RowCell
                Case GridHitTest.RowEdge
                Case GridHitTest.RowFooter
                Case Else
                    Return
            End Select
            FieldView.EditValue = GridView.GetDataRow(GridView.FocusedRowHandle)
            FieldView.ClosePopup()
        End Sub

#End Region


    End Class
End Namespace
